/**
 * Created by fly on 2017/1/17.
 */

$(function () {
    $('#demo').attr('href',ctx+'/download/download?fileName='+encodeURI('人天数据模版.xlsx'));

    var url1 = ctx+'/projectTasktime/upLoadExcel';
    var picker1Id = "picker";
    var uploader1 = createUploader(url1, picker1Id);
    initUploaderEvents(uploader1, "thelist");

    u = 0;
    $("#picker").click(function(){
        if(u==0){
            deleteFile(uploader1, "thelist");
        }
        u++;
    });

    $("#ctlBtn").click(function(){
        uploadFile(uploader1);
    });

    $("#delBtn").click(function(){
        deleteFile(uploader1, "thelist");
    });

    $("#ctlBtn").show();
    $("#delBtn").show();

});

function uploadFile(uploaderObject){

    for(var  i=0 ; i < uploaderObject.getFiles().length; i++){
        uploaderObject.upload(uploaderObject.getFiles[i]);
    }

}


function deleteFile(uploaderObject, listId){

    uploaderObject.reset();

    if($("#"+listId).children.length > 0 ){
        $("#"+listId).children().remove();
    }

}

function createUploader(url, pickerId){

    return WebUploader.create({

        // swf文件路径
        swf: ctx + '/resources/libs/webuploader-0.1.5/Uploader.swf',

        // 文件接收服务端。
        server: url,

        // 选择文件的按钮。可选。
        // 内部根据当前运行是创建，可能是input元素，也可能是flash.
        pick: '#' + pickerId,

        // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
        resize: false,

        fileNumLimit: 1,

        accept:{
            title: 'excel',
            extensions: 'xls,xlsx',
            mimeTypes: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        }
    });
}

function initUploaderEvents(uploaderObject, listId ){
    // 当有文件被添加进队列的时候
    uploaderObject.on( 'fileQueued', function( file ) {
        $("#" + listId).html( '<div id="' + file.id + '" class="item">' +
            '<h4 class="info">' + file.name + '</h4>' +
            '<p class="state">等待上传...</p>' +
            '</div>' );
        setTimeout(function(){
            u=0;
        },400);
    });

    // 文件上传过程中创建进度条实时显示。
    uploaderObject.on( 'uploadProgress', function( file, percentage ) {
        var $li = $( '#'+file.id ),
            $percent = $li.find('.progress .progress-bar');

        // 避免重复创建
        if ( !$percent.length ) {
            $percent = $('<div class="progress progress-striped active">' +
                '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                '</div>' +
                '</div>').appendTo( $li ).find('.progress-bar');
        }

        $li.find('p.state').text('上传中');

        $percent.css( 'width', percentage * 100 + '%' );
    });

    uploaderObject.on( 'uploadSuccess', function( file, response ) {
        if(response.code == 200){
            $( '#'+file.id ).find('p.state').text('已上传');
        }else{
            $( '#'+file.id ).find('p.state').text(response.msg).css({color:"red"});
        }

    });

    uploaderObject.on( 'uploadError', function( file ) {
        $( '#'+file.id ).find('p.state').text('上传出错');
    });

    uploaderObject.on( 'uploadComplete', function( file ) {
        $( '#'+file.id ).find('.progress').fadeOut();
    });

}
